package com.hue.springcloud.service.impl;

import com.hue.springcloud.domain.Order;
import com.hue.springcloud.mapper.OrderMapper;
import com.hue.springcloud.service.AccountService;
import com.hue.springcloud.service.OrderService;
import com.hue.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccountService accountService;
    @Autowired
    private StorageService storageService;

    //创建订单
    @Override
    //rollbackFor:哪些异常需要回滚
    //noRollbackFor:哪些异常不需要回滚
    @GlobalTransactional(name = "test",rollbackFor = Exception.class)
    public void createOrder() {
        //创建订单
        Order order = new Order();
        order.setUserId(1);
        order.setProductId(1);
        order.setCount(1);
        order.setMoney(new BigDecimal(100));
        order.setStatus(0);
        orderMapper.createOrder(order);
        //修改账户余额
        accountService.updateAccount(order.getUserId(),order.getMoney());
        int i=10/0;
        //修改库存
        storageService.updateStorage(order.getProductId(),order.getCount());
        //修改订单状态
        this.updateStatus(1,1);
    }

    @Override
    public void updateStatus(long id, int status) {
        orderMapper.updateStatus(id,status);
    }
}
